/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jxtaUtil;

import net.jxta.id.IDFactory;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeID;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Creates predictable ID's based on the hash of a
 * provided string.Provides an independent and
 * deterministic method for generating ID's
 */
public class IDCreator {
    private static final String SEED = "IDCreator";

    /**
     * Returns a SHA1 hash of a string.
     */
    private static byte[] hash(final String expression) {
        byte[] result;
        MessageDigest digest;

        if(expression == null) {
            throw new IllegalArgumentException("Invalid null expression");
        }

        try {
            digest = MessageDigest.getInstance("SHA1");
        } catch(NoSuchAlgorithmException failed) {
            failed.printStackTrace();
            RuntimeException failure = new IllegalStateException("Could not get" +
                    "SHA-1 Message");
            failure.initCause(failed);
            throw failure;
        }

        try {
            byte[] expressionBytes = expression.getBytes("UTF-8");
            result = digest.digest(expressionBytes);
        } catch(UnsupportedEncodingException impossible) {
            RuntimeException failure =
                    new IllegalStateException("Could not encode expression as UTF8");
            failure.initCause(impossible);
            throw failure;
        }
        return result;
    }

    /**
     * Given a pipe name, return a PipeID whose value is
     * chosen based upon that name.
     */
    public static PipeID createPipeID(PeerGroupID pgID, String pipeName) {
        String seed = pipeName + SEED;
        return IDFactory.newPipeID(pgID, hash(seed.toLowerCase()));
    }

    /**
     * Creates a group encoded random PipeID.
     */
    public static PipeID createNewPipeID(PeerGroupID pgID) {
        return IDFactory.newPipeID(pgID);
    }

    /**
     * Creates group encoded rancom PeerID
     */
    public static PeerID createNewPeerID(PeerGroupID pgID) {
        return IDFactory.newPeerID(pgID);
    }

    /**
     * Given a peer name generates a Peer ID whose value is
     * based upon that name.
     */
    public static PeerID createPeerID(PeerGroupID pgID, String peerName) {
        String seed = peerName + SEED;
        return IDFactory.newPeerID(pgID, hash(seed.toLowerCase()));
    }

    /**
     * Creates group encoded random PeerGroupID.
     */
    public static PeerGroupID createNewPeerGroupID(PeerGroupID pgID) {
        return IDFactory.newPeerGroupID(pgID);
    }

    /**
     * Given a group name generates a Peer Group ID whose vaue is
     * based upon that name.
     */
    public static PeerGroupID createPeerGroupID(final String groupName) {
        return IDFactory.newPeerGroupID(PeerGroupID.defaultNetPeerGroupID,
                hash(SEED + groupName.toLowerCase()));
    }

    /**
     * Constructs and returns a string encoded Infrastructure
     * PeerGroupID.
     */
    public static PeerGroupID createInfraPeerGroupID(String groupName) {
        return createPeerGroupID(groupName);
    }


}
